package com.railway.app;

import com.railway.classes.Staff;
import com.railway.classes.CashReconciliation;
import com.railway.utils.RestServer;
import com.railway.utils.TextFieldLimiter;
import com.railway.utils.Toast;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AbstractDocument;
import org.joda.time.DateTime;
import org.json.JSONException;
import org.json.JSONObject;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Moofie
 */
public class Main extends javax.swing.JFrame {

    CardLayout cl;
    public String position = null;
    public String loginUID = null;
    private final JButton okay = new JButton("Ok");
    private final JButton cancel = new JButton("Cancel");
    private JTextField field = new JTextField();
    private String txtField;

    public Main() {
        initComponents();
        cl = (CardLayout) jPanelContent.getLayout();
        jPanelContent.add(new TicketForm(), "ticket");
        jPanelContent.add(new StaffForm(), "staffMenu");
        jPanelContent.add(new ScheduleForm(), "schedule");
        jPanelContent.add(new OptionForm(), "option");
        jPanelContent.add(new ForgetPassword(), "forgetPassword");
        jPanelContent.add(new ReportForm(), "report");
        setButtonDisabled();
        AbstractDocument aDoc = (AbstractDocument) field.getDocument();
        aDoc.setDocumentFilter(new TextFieldLimiter("\\d{0,5}"));
        jtfIDUsername.setText("2");
        pfPassword.setText("123456");

        okay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane pane = getOptionPane((JComponent) e.getSource());
                pane.setValue(okay);
            }
        });
        okay.setEnabled(false);

        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane pane = getOptionPane((JComponent) e.getSource());
                pane.setValue(cancel);
            }
        });

        field.getDocument().addDocumentListener(new DocumentListener() {
            protected void update() {
                okay.setEnabled(field.getText().length() > 0);
                field.getText();
            }

            public void insertUpdate(DocumentEvent e) {
                update();
                field.getText();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                update();
                field.getText();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                update();
                field.getText();
            }
        });
    }

    protected JOptionPane getOptionPane(JComponent parent) {
        JOptionPane pane = null;
        if (!(parent instanceof JOptionPane)) {
            pane = getOptionPane((JComponent) parent.getParent());
        } else {
            pane = (JOptionPane) parent;
        }
        return pane;
    }

    private void setButtonEnabled() {
        btnTicketing.setEnabled(true);
        btnStaff.setEnabled(true);
        btnSchedule.setEnabled(true);
        btnReport.setEnabled(true);
        btnOption.setEnabled(true);
        btnSignOut.setEnabled(true);
    }

    private void setButtonDisabled() {
        btnTicketing.setEnabled(false);
        btnStaff.setEnabled(false);
        btnSchedule.setEnabled(false);
        btnReport.setEnabled(false);
        btnOption.setEnabled(false);
        btnSignOut.setEnabled(false);
    }

    private void authorityStaff() {
        this.btnTicketing.setEnabled(true);
        btnStaff.setEnabled(false);
        btnSchedule.setEnabled(false);
        btnReport.setEnabled(false);
        btnOption.setEnabled(true);
        btnSignOut.setEnabled(true);
    }

    private void authorityAdmin() {
        this.btnTicketing.setEnabled(true);
        btnStaff.setEnabled(true);
        btnSchedule.setEnabled(true);
        btnReport.setEnabled(true);
        btnOption.setEnabled(true);
        btnSignOut.setEnabled(true);
    }

    private double cashReconcilationTotal() {
        double total = 0;
        Map<String, Object> data = new HashMap<>();
        data.put("start", Staff.getLoginTime());
        data.put("end", Staff.getLogoutTime());

        String cR = RestServer.httpPost("getticket/salesamount", data);
        System.out.println(cR);
        try {
            JSONObject json = new JSONObject(cR);
            JSONObject content = json.getJSONObject("content");
            total = content.getDouble("total");
            CashReconciliation.setCashRecorded(total);
        } catch (JSONException ex) {
            System.out.println(ex);
        }
        return total;
    }

    private void cashReconcilationLog() {
        System.out.println("FieldTxt"+txtField);
        double cashRecorded =  0;
        double cashCollected = 0;
        double difference = 0;
        cashRecorded =  this.cashReconcilationTotal();
        cashCollected = Double.parseDouble(txtField);
        difference = cashCollected - cashRecorded;
        CashReconciliation.setCashCollected(Double.parseDouble(txtField));
        CashReconciliation.setDifferences(difference);
        JOptionPane.showMessageDialog(null,"cashRecorded: "+cashRecorded+"\ncashCollected: "+cashCollected+"\nCash Difference: "+ difference);
        
        System.out.println("Login Time : " + Staff.getLoginTime());
        System.out.println("Logout Time : " + Staff.getLogoutTime());

        String logDate = DateTime.now().toString("yyyy-MM-dd HH:mm:ss");
        Map<String, Object> data = new HashMap<>();
        data.put("cashCollected", cashCollected);
        data.put("cashRecorded", cashRecorded);
        data.put("differences", difference);
        data.put("logDate", logDate);
        data.put("personInCharge", this.getStaffIcNo());

        String cashRecon = RestServer.httpPost("insert/cashreconcilation", data);
        System.out.println("Recon" +cashRecon);
        try {
            JSONObject json = new JSONObject(cashRecon);
            //JSONObject content = json.getJSONObject("content");
            System.out.println(json.get("status"));
            if (json.getInt("status") != 200) {
                Toast.makeText(getParent(), "Error Occur, Cash Reconcilatiion unable to save!", Toast.LENGTH_LONG).display();
            }else{
                System.out.println("Cash Recon save succesfully");
            }
        } catch (JSONException ex) {
            System.out.println(ex);
        }
    }

    private String getStaffIcNo() {
        String staffIcNo = null;
        String staffs = RestServer.httpGet("staffIC", Staff.getUid());
        System.out.println(staffs);
        try {
            JSONObject json = new JSONObject(staffs);
            JSONObject content = json.getJSONObject("content");
            staffIcNo = content.getString("icNo");
        } catch (JSONException ex) {
            System.out.println("Staff ID - > IC"+ex);
        }
        return staffIcNo;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelNavigation = new javax.swing.JPanel();
        sideNavigation = new javax.swing.JPanel();
        lblNavigation = new javax.swing.JLabel();
        btnTicketing = new javax.swing.JButton();
        btnStaff = new javax.swing.JButton();
        btnSchedule = new javax.swing.JButton();
        btnReport = new javax.swing.JButton();
        btnSignOut = new javax.swing.JButton();
        btnOption = new javax.swing.JButton();
        jPanelContent = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jlblPassword = new javax.swing.JLabel();
        jtfIDUsername = new javax.swing.JTextField();
        pfPassword = new javax.swing.JPasswordField();
        jcbIDUsername = new javax.swing.JComboBox();
        jbtLogOn = new javax.swing.JButton();
        jbtForgetPassword = new javax.swing.JButton();
        jlblTitle = new javax.swing.JLabel();
        jPanelTop6 = new javax.swing.JPanel();
        jlblTitle6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));
        setFocusable(false);
        setResizable(false);

        jPanelNavigation.setBackground(new java.awt.Color(204, 204, 204));

        sideNavigation.setBackground(new java.awt.Color(46, 204, 113));

        lblNavigation.setFont(new java.awt.Font("Berlin Sans FB", 0, 18)); // NOI18N
        lblNavigation.setText("  Navigation");

        btnTicketing.setBackground(new java.awt.Color(166, 155, 205));
        btnTicketing.setFont(new java.awt.Font("Berlin Sans FB", 0, 18)); // NOI18N
        btnTicketing.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/railway/image/icon/ticket-icon.png"))); // NOI18N
        btnTicketing.setText("Ticketing");
        btnTicketing.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTicketingActionPerformed(evt);
            }
        });

        btnStaff.setBackground(new java.awt.Color(166, 155, 205));
        btnStaff.setFont(new java.awt.Font("Berlin Sans FB", 0, 18)); // NOI18N
        btnStaff.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/railway/image/icon/staff-icon.png"))); // NOI18N
        btnStaff.setText("Staff");
        btnStaff.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStaffActionPerformed(evt);
            }
        });

        btnSchedule.setBackground(new java.awt.Color(166, 155, 205));
        btnSchedule.setFont(new java.awt.Font("Berlin Sans FB", 0, 18)); // NOI18N
        btnSchedule.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/railway/image/icon/schedule-icon.png"))); // NOI18N
        btnSchedule.setText("Schedule");
        btnSchedule.setToolTipText("");
        btnSchedule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnScheduleActionPerformed(evt);
            }
        });

        btnReport.setBackground(new java.awt.Color(166, 155, 205));
        btnReport.setFont(new java.awt.Font("Berlin Sans FB", 0, 18)); // NOI18N
        btnReport.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/railway/image/icon/report-icon.png"))); // NOI18N
        btnReport.setText("Report");
        btnReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReportActionPerformed(evt);
            }
        });

        btnSignOut.setBackground(new java.awt.Color(166, 155, 205));
        btnSignOut.setFont(new java.awt.Font("Berlin Sans FB", 0, 18)); // NOI18N
        btnSignOut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/railway/image/icon/logout-icon.png"))); // NOI18N
        btnSignOut.setText("Sign Out");
        btnSignOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSignOutActionPerformed(evt);
            }
        });

        btnOption.setBackground(new java.awt.Color(166, 155, 205));
        btnOption.setFont(new java.awt.Font("Berlin Sans FB", 0, 18)); // NOI18N
        btnOption.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/railway/image/icon/Setting-icon.png"))); // NOI18N
        btnOption.setText("Option");
        btnOption.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOptionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout sideNavigationLayout = new javax.swing.GroupLayout(sideNavigation);
        sideNavigation.setLayout(sideNavigationLayout);
        sideNavigationLayout.setHorizontalGroup(
            sideNavigationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, sideNavigationLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(sideNavigationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnOption, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(sideNavigationLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(sideNavigationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnSignOut, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnReport, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnSchedule, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnStaff, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnTicketing, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblNavigation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(805, 805, 805))
        );
        sideNavigationLayout.setVerticalGroup(
            sideNavigationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(sideNavigationLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(lblNavigation)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnTicketing)
                .addGap(12, 12, 12)
                .addComponent(btnStaff)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSchedule)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnReport)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnOption)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSignOut)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnTicketing.getAccessibleContext().setAccessibleName("btnTicketing");

        javax.swing.GroupLayout jPanelNavigationLayout = new javax.swing.GroupLayout(jPanelNavigation);
        jPanelNavigation.setLayout(jPanelNavigationLayout);
        jPanelNavigationLayout.setHorizontalGroup(
            jPanelNavigationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(sideNavigation, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanelNavigationLayout.setVerticalGroup(
            jPanelNavigationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(sideNavigation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jPanelContent.setBackground(new java.awt.Color(46, 204, 113));
        jPanelContent.setLayout(new java.awt.CardLayout());

        jPanel1.setBackground(new java.awt.Color(46, 204, 113));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Login"));

        jlblPassword.setText(" Password");

        jcbIDUsername.setBackground(new java.awt.Color(46, 204, 113));
        jcbIDUsername.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ID", "Username" }));

        jbtLogOn.setBackground(new java.awt.Color(46, 204, 113));
        jbtLogOn.setText("Log On");
        jbtLogOn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtLogOnActionPerformed(evt);
            }
        });

        jbtForgetPassword.setBackground(new java.awt.Color(46, 204, 113));
        jbtForgetPassword.setText("Forget Password");
        jbtForgetPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtForgetPasswordActionPerformed(evt);
            }
        });

        jlblTitle.setFont(new java.awt.Font("MS PMincho", 0, 36)); // NOI18N
        jlblTitle.setText("Railway Ticketing System");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(233, Short.MAX_VALUE)
                .addComponent(jlblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 445, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(200, 200, 200))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(242, 242, 242)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jcbIDUsername, 0, 162, Short.MAX_VALUE)
                            .addComponent(jlblPassword, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfIDUsername, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(pfPassword, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(328, 328, 328)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jbtLogOn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbtForgetPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(69, 69, 69)
                .addComponent(jlblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(70, 70, 70)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfIDUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbIDUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblPassword)
                    .addComponent(pfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jbtLogOn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtForgetPassword)
                .addContainerGap(205, Short.MAX_VALUE))
        );

        jPanelContent.add(jPanel1, "login");

        jPanelTop6.setBackground(new java.awt.Color(46, 204, 113));

        javax.swing.GroupLayout jPanelTop6Layout = new javax.swing.GroupLayout(jPanelTop6);
        jPanelTop6.setLayout(jPanelTop6Layout);
        jPanelTop6Layout.setHorizontalGroup(
            jPanelTop6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTop6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlblTitle6, javax.swing.GroupLayout.PREFERRED_SIZE, 479, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelTop6Layout.setVerticalGroup(
            jPanelTop6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTop6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlblTitle6)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelTop6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelNavigation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelContent, javax.swing.GroupLayout.PREFERRED_SIZE, 890, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelTop6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelNavigation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelContent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        jPanelNavigation.getAccessibleContext().setAccessibleName("jpMain");
        jPanelNavigation.getAccessibleContext().setAccessibleDescription("");

        getAccessibleContext().setAccessibleName("jPanelMain");

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSignOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSignOutActionPerformed
        if (Staff.isTransaction()) {
            String logDT = DateTime.now().toString("yyyy-MM-dd HH:mm:ss");
            Staff.setLogoutTime(logDT);
            int result = JOptionPane.showOptionDialog(
                    null,
                    field,
                    "Enter Cash Collected",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    new Object[]{okay, cancel},
                    okay);
            txtField = field.getText();
            //result 0 = Okay else Cancel
            if (result == 0) {
                field.setText(null);
                cashReconcilationLog();
                Toast.makeText(getParent(), "You have succesfully logged out!", Toast.LENGTH_LONG).display();
                cl.show(jPanelContent, "login");
                setButtonDisabled();
                btnTicketing.setBackground(new Color(166, 155, 205));
                btnStaff.setBackground(new Color(166, 155, 205));
                btnSchedule.setBackground(new Color(166, 155, 205));
                btnReport.setBackground(new Color(166, 155, 205));
                btnOption.setBackground(new Color(166, 155, 205));
                btnSignOut.setBackground(new Color(166, 155, 205));
                CashReconciliationDialog cR = new CashReconciliationDialog(null,false);
                cR.show();
            } else {
                btnSignOut.setBackground(new Color(166, 155, 205));
            }
        } else {
            Toast.makeText(getParent(), "You have succesfully logged out!", Toast.LENGTH_LONG).display();
            cl.show(jPanelContent, "login");
            setButtonDisabled();
            btnTicketing.setBackground(new Color(166, 155, 205));
            btnStaff.setBackground(new Color(166, 155, 205));
            btnSchedule.setBackground(new Color(166, 155, 205));
            btnReport.setBackground(new Color(166, 155, 205));
            btnOption.setBackground(new Color(166, 155, 205));
            btnSignOut.setBackground(new Color(166, 155, 205));
        }
    }//GEN-LAST:event_btnSignOutActionPerformed

    private void btnReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReportActionPerformed
        cl.show(jPanelContent, "report");
        btnTicketing.setBackground(new Color(166, 155, 205));
        btnStaff.setBackground(new Color(166, 155, 205));
        btnSchedule.setBackground(new Color(166, 155, 205));
        btnReport.setBackground(new Color(255, 0, 255));
        btnOption.setBackground(new Color(166, 155, 205));
        btnSignOut.setBackground(new Color(166, 155, 205));
    }//GEN-LAST:event_btnReportActionPerformed

    private void btnScheduleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnScheduleActionPerformed
        cl.show(jPanelContent, "schedule");
        btnTicketing.setBackground(new Color(166, 155, 205));
        btnStaff.setBackground(new Color(166, 155, 205));
        btnSchedule.setBackground(new Color(255, 0, 255));
        btnReport.setBackground(new Color(166, 155, 205));
        btnOption.setBackground(new Color(166, 155, 205));
        btnSignOut.setBackground(new Color(166, 155, 205));
    }//GEN-LAST:event_btnScheduleActionPerformed

    private void btnStaffActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStaffActionPerformed

        cl.show(jPanelContent, "staffMenu");

        btnTicketing.setBackground(new Color(166, 155, 205));
        btnStaff.setBackground(new Color(255, 0, 255));
        btnSchedule.setBackground(new Color(166, 155, 205));
        btnReport.setBackground(new Color(166, 155, 205));
        btnOption.setBackground(new Color(166, 155, 205));
        btnSignOut.setBackground(new Color(166, 155, 205));
    }//GEN-LAST:event_btnStaffActionPerformed

    private void btnTicketingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTicketingActionPerformed
        cl.show(jPanelContent, "ticket");

        btnTicketing.setBackground(new Color(255, 0, 255));
        btnStaff.setBackground(new Color(166, 155, 205));
        btnSchedule.setBackground(new Color(166, 155, 205));
        btnReport.setBackground(new Color(166, 155, 205));
        btnOption.setBackground(new Color(166, 155, 205));
        btnSignOut.setBackground(new Color(166, 155, 205));

    }//GEN-LAST:event_btnTicketingActionPerformed

    private void btnOptionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOptionActionPerformed
        cl.show(jPanelContent, "option");
        btnTicketing.setBackground(new Color(166, 155, 205));
        btnStaff.setBackground(new Color(166, 155, 205));
        btnSchedule.setBackground(new Color(166, 155, 205));
        btnReport.setBackground(new Color(166, 155, 205));
        btnOption.setBackground(new Color(255, 0, 255));
        btnSignOut.setBackground(new Color(166, 155, 205));
    }//GEN-LAST:event_btnOptionActionPerformed

    private void jbtLogOnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtLogOnActionPerformed
        Staff.setUid(jtfIDUsername.getText().toString());
        if (jtfIDUsername.getText().length() != 0) {
            if (pfPassword.getPassword().length != 0) {
                if (jcbIDUsername.getSelectedItem().toString().equals("ID")) {
                    String password = new String(pfPassword.getPassword());
                    Map<String, Object> data = new HashMap<>();
                    data.put("uid", jtfIDUsername.getText());
                    data.put("password", password);

                    String a = RestServer.httpPost("loginStaff", data);
                    System.out.println(a);
                    try {
                        JSONObject json = new JSONObject(a);
                        JSONObject content = json.getJSONObject("content");

                        if (content.getBoolean("login")) {
                            if (content.getString("position").equals("Admin")) {
                                if (content.getString("status").equalsIgnoreCase("Available")) {
                                    Toast.makeText(getParent(), "Welcome, " + content.getString("name"), Toast.LENGTH_LONG).display();
                                    this.position = "Admin";
                                    authorityAdmin();
                                    Staff.setTransaction(false);
                                    jtfIDUsername.setText(null);
                                    pfPassword.setText(null);
                                    String newLoginTime = DateTime.now().toString("yyyy-MM-dd HH:mm:ss");
                                    Staff.setLoginTime(newLoginTime);
                                    System.out.print(Staff.getLoginTime());
                                    cl.show(jPanelContent, "ticket");
                                    btnTicketing.setBackground(new Color(255, 0, 255));
                                } else if (content.getString("status").equalsIgnoreCase("Retired")) {
                                    Toast.makeText(getParent(), "Login denied - Account Retired", Toast.LENGTH_LONG).display();
                                } else if (content.getString("status").equalsIgnoreCase("Deleted")) {
                                    Toast.makeText(getParent(), "Login denied - Account Deleted", Toast.LENGTH_LONG).display();
                                }
                            } else {
                                if (content.getString("status").equalsIgnoreCase("Available")) {
                                    Toast.makeText(getParent(), "Welcome, " + content.getString("name"), Toast.LENGTH_LONG).display();
                                    this.position = "Staff";
                                    authorityStaff();
                                    Staff.setTransaction(false);
                                    jtfIDUsername.setText(null);
                                    pfPassword.setText(null);
                                    String newLoginTime2 = DateTime.now().toString("yyyy-MM-dd HH:mm:ss");
                                    Staff.setLoginTime(newLoginTime2);
                                    System.out.print(Staff.getLoginTime());
                                    cl.show(jPanelContent, "ticket");
                                    btnTicketing.setBackground(new Color(255, 0, 255));
                                } else if (content.getString("status").equalsIgnoreCase("Retired")) {
                                    Toast.makeText(getParent(), "Login denied - Account Retired", Toast.LENGTH_LONG).display();
                                } else if (content.getString("status").equalsIgnoreCase("Deleted")) {
                                    Toast.makeText(getParent(), "Login denied - Account Deleted", Toast.LENGTH_LONG).display();
                                }
                            }
                        } else if (!content.getBoolean("login")) {
                            switch (content.getString("err")) {
                                case "uid":
                                    Toast.makeText(getParent(), content.getString("message"), Toast.LENGTH_LONG).display();
                                    break;
                                case "pass":
                                    Toast.makeText(getParent(), content.getString("message"), Toast.LENGTH_LONG).display();
                                    break;
                            }
                        }
                    } catch (JSONException ex) {
                        System.out.println(ex);
                    }
                } else {
                    String password = new String(pfPassword.getPassword());
                    Map<String, Object> data = new HashMap<>();
                    data.put("username", jtfIDUsername.getText());
                    data.put("password", password);

                    String a = RestServer.httpPost("loginStaffByUsername", data);
                    System.out.println(a);
                    try {
                        JSONObject json = new JSONObject(a);
                        JSONObject content = json.getJSONObject("content");

                        if (content.getBoolean("login")) {
                            if (content.getString("position").equals("Admin")) {
                                if (content.getString("status").equalsIgnoreCase("Available")) {
                                    Toast.makeText(getParent(), "Welcome, " + content.getString("name"), Toast.LENGTH_LONG).display();
                                    this.position = "Admin";
                                    authorityAdmin();
                                    Staff.setTransaction(false);
                                    jtfIDUsername.setText(null);
                                    pfPassword.setText(null);
                                    String newLoginTime3 = DateTime.now().toString("yyyy-MM-dd HH:mm:ss");
                                    Staff.setLoginTime(newLoginTime3);
                                    System.out.print(Staff.getLoginTime());
                                    cl.show(jPanelContent, "ticket");
                                    btnTicketing.setBackground(new Color(255, 0, 255));
                                } else if (content.getString("status").equalsIgnoreCase("Retired")) {
                                    Toast.makeText(getParent(), "Login denied - Account Retired", Toast.LENGTH_LONG).display();
                                } else if (content.getString("status").equalsIgnoreCase("Deleted")) {
                                    Toast.makeText(getParent(), "Login denied - Account Deleted", Toast.LENGTH_LONG).display();
                                }
                            } else {
                                if (content.getString("status").equalsIgnoreCase("Available")) {
                                    Toast.makeText(getParent(), "Welcome, " + content.getString("name"), Toast.LENGTH_LONG).display();
                                    this.position = "Staff";
                                    authorityStaff();
                                    Staff.setTransaction(false);
                                    jtfIDUsername.setText(null);
                                    pfPassword.setText(null);
                                    String newLoginTime4 = DateTime.now().toString("yyyy-MM-dd HH:mm:ss");
                                    Staff.setLoginTime(newLoginTime4);
                                    System.out.print(Staff.getLoginTime());
                                    cl.show(jPanelContent, "ticket");
                                    btnTicketing.setBackground(new Color(255, 0, 255));
                                } else if (content.getString("status").equalsIgnoreCase("Retired")) {
                                    Toast.makeText(getParent(), "Login denied - Account Retired", Toast.LENGTH_LONG).display();
                                } else if (content.getString("status").equalsIgnoreCase("Deleted")) {
                                    Toast.makeText(getParent(), "Login denied - Account Deleted", Toast.LENGTH_LONG).display();
                                }
                            }
                        } else if (!content.getBoolean("login")) {
                            switch (content.getString("err")) {
                                case "uid":
                                    Toast.makeText(getParent(), content.getString("message"), Toast.LENGTH_LONG).display();
                                    break;
                                case "pass":
                                    Toast.makeText(getParent(), content.getString("message"), Toast.LENGTH_LONG).display();
                                    break;
                            }
                        }
                    } catch (JSONException ex) {
                        System.out.println(ex);
                    }
                }
            } else {
                Toast.makeText(getParent(), "Please input password", Toast.LENGTH_LONG).display();
            }
        } else {
            Toast.makeText(getParent(), "Please input ID/Username", Toast.LENGTH_LONG).display();

        }
    }//GEN-LAST:event_jbtLogOnActionPerformed

    private void jbtForgetPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtForgetPasswordActionPerformed
        cl.show(jPanelContent, "forgetPassword");
    }//GEN-LAST:event_jbtForgetPasswordActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnOption;
    private javax.swing.JButton btnReport;
    private javax.swing.JButton btnSchedule;
    private javax.swing.JButton btnSignOut;
    private javax.swing.JButton btnStaff;
    private javax.swing.JButton btnTicketing;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanelContent;
    private javax.swing.JPanel jPanelNavigation;
    private javax.swing.JPanel jPanelTop6;
    private javax.swing.JButton jbtForgetPassword;
    private javax.swing.JButton jbtLogOn;
    private javax.swing.JComboBox jcbIDUsername;
    private javax.swing.JLabel jlblPassword;
    private javax.swing.JLabel jlblTitle;
    private javax.swing.JLabel jlblTitle6;
    private javax.swing.JTextField jtfIDUsername;
    private javax.swing.JLabel lblNavigation;
    private javax.swing.JPasswordField pfPassword;
    private javax.swing.JPanel sideNavigation;
    // End of variables declaration//GEN-END:variables
}
